package com.zwj.web;

import com.google.gson.Gson;
import com.zwj.bean.User;
import com.zwj.service.Impl.UserServiceImpl;
import com.zwj.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;
/**
 * @author zwj
 * @create 2021-05-13-9:28
 */
public class UserServlet extends BaseServlet {
    private UserService userService = new UserServiceImpl();

    /**
     * 注册，/pages/user/regist.jsp的form表单提交请求访问
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取 Session 中的验证码
        String token = (String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        // 删除 Session 中的验证码
        req.getSession().removeAttribute(KAPTCHA_SESSION_KEY);
        //获取请求的参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        String code = req.getParameter("code");
        //判断验证码是否正确
        if(token != null && token.equalsIgnoreCase(code)){
            //判断用户名是否存在
            if(userService.existsUsername(username)){
                //用户存在
                System.out.println("用户已已存在");
                //数据回写
                req.setAttribute("msg","用户名已存在");
                req.setAttribute("username",username);
                req.setAttribute("email",email);
                req.getRequestDispatcher("/pages/user/regist.jsp").forward(req,resp);
            }else {
                //成功 注册
                userService.registUser(new User(null,username,password,email));
                //请求转发成功页面
                req.getRequestDispatcher("/pages/user/regist_success.jsp").forward(req,resp);
            }
        }else{
            //验证码错误
            System.out.println("验证码错误");
            //回写数据
            req.setAttribute("msg","验证码错误");
            req.setAttribute("username",username);
            req.setAttribute("email",email);
            //请求转发回来
            req.getRequestDispatcher("/pages/user/regist.jsp").forward(req,resp);
        }

    }

    /**
     * 登录,/pages/user/login.jsp的form表单提交请求访问
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求的参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User login = userService.login(new User(null, username, password, null));
        //判断用户
        if(login != null){
            req.getSession().setAttribute("user",login);
            //登录成功，请求转发到成功页面
            req.getRequestDispatcher("/pages/user/login_success.jsp").forward(req,resp);
        }else {
            //登录失败，回写数据
            req.setAttribute("msg","用户名或密码错误");
            req.setAttribute("username",username);
            //请求转发回登录页面
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req,resp);
        }
    }

    /**
     * 登出,login_sucess_menu.jsp的注销请求访问
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //销毁session域中的数据
        req.getSession().invalidate();
        //重定向回首页
        resp.sendRedirect(req.getContextPath());
    }

    /**
     * AJAX
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void ajaxExistsUsername(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取请求的参数 username
        String username = req.getParameter("username");
        // 调用 userService.existsUsername();
        boolean existsUsername = userService.existsUsername(username);
        // 把返回的结果封装成为 map 对象
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("existsUsername",existsUsername);
        Gson gson = new Gson();
        String json = gson.toJson(resultMap);
        resp.getWriter().write(json);
    }
}
